package main

import (
	"encoding/json"
	"os"
	"fmt"
)

type Girl struct {
	Name string
	Age int
	Sex bool
	Hobby []string
}

func main021() {
	outFile, _ := os.OpenFile("C:/Users/28457/Desktop/王吕丹的朋友们.json", os.O_CREATE|os.O_WRONLY, 0666)
	defer outFile.Close()
	encoder := json.NewEncoder(outFile)

	girls := make([]*Girl,0)
	girls = append(girls, &Girl{"王同丹", 24, false, []string{"抽大麻", "喝茶", "泡仔"}})
	girls = append(girls, &Girl{"王铁蛋", 23, false, []string{"抽大麻", "喝茶", "泡仔"}})
	girls = append(girls, &Girl{"旺西单", 22, false, []string{"抽大麻", "喝茶", "泡仔"}})

	err := encoder.Encode(girls)
	if err != nil {
		fmt.Println("编码失败，err=", err)
	} else {
		fmt.Println("编码成功！")
	}
}

func main022() {
	inFile, _ := os.OpenFile("C:/Users/28457/Desktop/王吕丹的朋友们.json", os.O_RDONLY, 0666)
	defer inFile.Close()
	decoder := json.NewDecoder(inFile)

	girls := make([]*Girl, 0)
	err := decoder.Decode(&girls)
	if err!=nil{
		fmt.Println("解码JSON文件失败,err=",err)
	}else{
		fmt.Println("解码JSON文件成功！data=",girls)
		for _,gp := range girls{
			fmt.Println(*gp)
		}
	}
}
